package com.doit.test;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManagementSystem {
    public static void main(String[] args) {
        ArrayList<Student> list = new ArrayList<>();//创建学生集合
        showMenu();//菜单显示界面

        Student s = new Student();
        s.id ="001";
        s.name="赵露思";
        s.age = "22";
        s.address ="北京";
        list.add(s);

        Student ss = new Student();
        ss.id ="002";
        ss.name="杨幂";
        ss.age = "23";
        ss.address ="北京";
        list.add(ss);

        Student sss = new Student();
        sss.id ="003";
        sss.name="宋茜";
        sss.age = "23";
        sss.address ="北京";
        list.add(sss);

        Scanner sc = new Scanner(System.in);

        while(true)
        {
            int k=getKeyboard();
            switch(k){
                case 1:addStudent(list);//添加学生
                    break;
                case 2:delStudent(list);//删除学生
                    break;
                case 3:modStudent(list);//修改学生
                    break;
                case 4:seeStudent(list);//查看学生
                    break;
                case 5:exitSystem();//退出系统
                    break;
                default:otherKeyboard();
                    break;
            }
        }
    }
    public static void modStudent(ArrayList<Student>list)
    {
        modOneStudent(list);//修改一个学生信息的方法
        while(true)
        {
            Scanner a = new Scanner(System.in);
            char aa =a.next().charAt(0);
            if(aa==49)
                modOneStudent(list);
            else if(aa==50)
            {
                break ;
            }
            else //if((aa!=1) || (aa!=2))
                System.out.println("请正确输入数字:");
        }
        showMenu();
    }
    public static void modOneStudent(ArrayList<Student>list)
    {
        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        System.out.println("请输入学号:");
        s.id = sc.next();
        //判断一下这个 学号有没有用过
        //0没有1有
        int flag = isUseId(s.id,list);
        while(flag==0)
        {
            System.out.println("未找到学生信息,请重新输入学号:");
            s.id = sc.next();
//            s.name=s.name;
//            s.age=s.age;
//            s.address=s.address;
            int flagg = isUseId(s.id,list);
            if(flagg==1)
                flag=1;
        }
        for (int i = 0; i < list.size(); i++) {
            Student S = new Student();
            S=list.get(i);
            if(s.id.equals(S.id))
            {
                s= list.get(i);
                choiceModOneStudent(s,i,list);//选择修改的选项
//                System.out.println("请修改学生姓名:");
//                s.name =sc.next();
//                System.out.println("请修改学生年龄:");
//                s.age = sc.next();
//                System.out.println("请修改学生居住地");
//                s.address=sc.next();
//                list.add(i,s);//添加进入指定集合
//                list.remove(i+1);//删除原来的信息
//                System.out.println("修改完毕,继续修改请按1,返回请按2");//上野联动,一死一送
                break;
            }
        }

    }
    public static void choiceModOneStudent(Student s,int i ,ArrayList<Student>list)
    {
        //思路:先记录原先的信息,在修改其中一个信息,再把原先的未修改的信息添加进去
        Student temp = new Student();
        temp.id = s.id;
        temp.name =s.name;
        temp.age=s.age;
        temp.address=s.address;

        Scanner sc = new Scanner(System.in);
        System.out.println("请选择您要修改的内容");
        System.out.println("1:学号 2:姓名 3:年龄 4:居住地 5 :全部修改");
        char a = sc.next().charAt(0);
        switch(a){
            case 49:
                System.out.println("请修改学生学号:");
                s.id =sc.next();
                s.name = temp.name;
                s.age = temp.age;
                s.address = temp.address;
                list.add(i,s);//添加进入指定集合
                list.remove(i+1);//删除原来的信息
                System.out.println("修改完毕,继续修改请按1,返回请按2");//上野联动,一死一送
                break;
            case 50:
                System.out.println("请修改学生姓名:");
                s.name =sc.next();
                s.id =temp.id;
                s.age = temp.age;
                s.address = temp.address;
                list.add(i,s);//添加进入指定集合
                list.remove(i+1);//删除原来的信息
                System.out.println("修改完毕,继续修改请按1,返回请按2");//上野联动,一死一送
                break;
            case 51:
                System.out.println("请修改学生年龄:");
                s.age = sc.next();
                s.id =temp.id;
                s.name = temp.name;
                s.address = temp.address;
                list.add(i,s);//添加进入指定集合
                list.remove(i+1);//删除原来的信息
                System.out.println("修改完毕,继续修改请按1,返回请按2");//上野联动,一死一送
                break;
            case 52:
                System.out.println("请修改学生居住地");
                s.address=sc.next();
                s.id =temp.id;
                s.name = temp.name;
                s.age = temp.age;
                list.add(i,s);//添加进入指定集合
                list.remove(i+1);//删除原来的信息
                System.out.println("修改完毕,继续修改请按1,返回请按2");//上野联动,一死一送
                break;
            case 53:
                System.out.println("请修改学生姓名:");
                s.name =sc.next();
                System.out.println("请修改学生年龄:");
                s.age = sc.next();
                System.out.println("请修改学生居住地");
                s.address=sc.next();
                list.add(i,s);//添加进入指定集合
                list.remove(i+1);//删除原来的信息
                System.out.println("修改完毕,继续修改请按1,返回请按2");//上野联动,一死一送
                break;
            default:
                System.out.println("请正确选择");
                choiceModOneStudent(s,i,list);//选择修改的选项
                list.add(i,s);//添加进入指定集合
                list.remove(i+1);//删除原来的信息
                System.out.println("修改完毕,继续修改请按1,返回请按2");//上野联动,一死一送
                break;
        }
    }
    public static void delStudent(ArrayList<Student>list)
    {
        if(list.size()==0)
        {
            System.out.println("您还没有录入学生信息");
            System.out.println("按任意键返回菜单");
            Scanner sc = new Scanner(System.in);
            if(sc.next()!=null)
                showMenu();
        }
        else
        {
                        Scanner sc = new Scanner(System.in);
                        System.out.println("请输入要删除学生信息的学号:");
                         String a = sc.next();
                        //判断一下这个 学号有没有
                        int flag = haveId(a,list);
                        while(flag==0)
                        {
                            System.out.println("未查询到此学号,请重新输入学号:");
                            a = sc.next();
                            int flagg = haveId(a,list);//0没有1有
                            if(flagg==1)
                                flag=1;
                        }
                        System.out.println("查询到您要删除的学号,确定删除吗?Y/N");

                        a:while(true)
                        {
                            String key = sc.next();
                            if("Y".equals(key))
                            {
                                delOneStudent(a,list);
                                //System.out.println("拜拜了您呐!");
                                //System.exit(0);//退出系统
                                break a;// 退出当前while语句
                            }
                            else if (key.equals("N"))
                            {
                                System.out.println("已取消删除信息,返回学生管理系统");
            //                showMenu();
                                break a;
                            }
                            else
                                System.out.println("请正确输入");
                        }
                        showMenu();
        }
    }
    public static void delOneStudent(String a ,ArrayList<Student>list)
    {
        for (int i = 0; i < list.size(); i++) {
            Student s = new Student();
            s=list.get(i);
            if(a.equals(s.id))
            {
                list.remove(i);//删除id所在元素位置的信息
                System.out.println("学号为:"+a+"的学生信息已被删除!");
                System.out.println("删除完毕,继续删除请按1,返回请按2");//野辅联动,一死一送
                if(panduan(list))
                    break;
            }
        }
    }
    public static boolean panduan (ArrayList<Student>list)
    {
        while(true)
        {
            Scanner a = new Scanner(System.in);
            char aa =a.next().charAt(0);
            if(aa==49)
            {
                delStudent(list);//删除一个学生的信息
                return false;//因为是子方法调用主方法,索引要返回false来结束while语句
            }

            else if(aa==50)
            {
                return true;
            }
            else //if((aa!=1) || (aa!=2))
                System.out.println("请正确输入数字:");
        }
    }
    public static int haveId(String a,ArrayList<Student>list)
    {
        int flag = 0;//0没有1有
        for (int i = 0; i < list.size(); i++) {
            Student s = new Student();
            s=list.get(i);
            if(a.equals(s.id))
            {
                flag = 1;
            }
        }
        return flag;
    }
    public static void addStudent(ArrayList<Student>list)
    {
        addOneStudent(list);//存储一个学生信息的方法
        while(true)
        {
            Scanner a = new Scanner(System.in);
            char aa =a.next().charAt(0);
            if(aa==49)
                addOneStudent(list);
            else if(aa==50)
            {
                break ;
            }
            else //if((aa!=1) || (aa!=2))
                System.out.println("请正确输入数字:");
        }
        showMenu();

    }
    public static void addOneStudent(ArrayList<Student>list)
    {
        Scanner sc = new Scanner(System.in);
        Student s = new Student();
        System.out.println("请输入学号:");
        s.id = sc.next();
        //判断一下这个 学号有没有用过
        int flag = isUseId(s.id,list);
        while(flag==1)
        {
            System.out.println("学号已被占用,请重新输入学号:");
            s.id = sc.next();
            int flagg = isUseId(s.id,list);
            if(flagg!=1)
                flag=0;
        }
        System.out.println("请输入学生姓名:");
        s.name =sc.next();
        System.out.println("请输入学生年龄:");
        s.age = sc.next();
        System.out.println("请输入学生居住地");
        s.address=sc.next();
        list.add(s);//添加进入集合
        System.out.println("添加完毕,继续添加请按1,返回请按2");//中野联动,一死一送
    }
    public static int isUseId(String a,ArrayList<Student>list)
    {
        int flag = 0;//0没有重复1重复
        for (int i = 0; i < list.size(); i++) {
            Student s = new Student();
            s=list.get(i);
            if(a.equals(s.id))
            {
                flag = 1;
            }
        }
        return flag;
    }
    public static void seeStudent(ArrayList<Student>list)
    {
        if(list.size()==0)
        {
            System.out.println("您还没有录入学生信息");
            System.out.println("按任意键返回菜单");
            Scanner sc = new Scanner(System.in);
            if(sc.next()!=null)
                showMenu();
        }

        else
        {
            System.out.println("学号 \t姓名\t年龄\t居住地");
            for (int i = 0; i < list.size(); i++) {
                Student s =list.get(i);
                System.out.println(s.id+"\t\t"+s.name+" \t"+s.age+"\t\t"+s.address );
            }
            System.out.println("全部信息查看完毕");
            System.out.println("按任意键返回菜单");
            Scanner sc = new Scanner(System.in);
            if(sc.next()!=null)
                showMenu();
        }
    }
    public static void exitSystem()
    {
        Scanner sc = new Scanner(System.in);
        System.out.println("您确定要退出系统吗?Y/N");
        a:while(true)
        {
            String key = sc.next();
            if("Y".equals(key))
            {
                System.out.println("拜拜了您呐!");
                System.exit(0);//退出系统
                //break a;// 退出当前while语句
            }
            else if (key.equals("N"))
            {
                System.out.println("继续浏览学生管理系统");
                showMenu();
                break a;
            }
            else
                System.out.println("请正确输入");
        }

    }
    public static void otherKeyboard(){
        System.out.println("菜单选项是1-5,请重新输入");
        showMenu();//菜单显示界面
    }
    public static int getKeyboard()
    {
        Scanner sc = new Scanner(System.in);
        return sc.nextInt();
    }
    public static void showMenu()
    {
        System.out.println("--------------学生管理系统-------------");
        System.out.println("1 . 添加学生 ");
        System.out.println("2 . 删除学生 ");
        System.out.println("3 . 修改学生 ");
        System.out.println("4 . 查看学生 ");
        System.out.println("5 . 退出系统 ");

    }
}
